package com.codebuffer.nowcoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class PrintListFromTailToHead {
    public static void main(String[] args) {
        int a[] = {67, 0, 24, 58};
        ListNode listNode2 =null;
        for (int i = a.length - 1; i >=0; i--) {
            ListNode listNode = new ListNode(a[i]);
            if(null!=listNode2){
                listNode.next=listNode2;
            }
            listNode2 = listNode;
        }
        System.out.println(printListFromTailToHead(listNode2));
    }

    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> integers= new ArrayList<>();
        if(null==listNode){
            return integers;
        }
        Stack<Integer> stack = new Stack<>();
        do{
            stack.push(listNode.val);
            listNode = listNode.next;
        }while (null!=listNode);

        while (stack.size()>0){
            integers.add(stack.pop());
        }
        return integers;
    }

    public static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
}
